package org.check.SQL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import org.check.SSH.JumpHosts;
import org.check.VO.DBConnVO;
import org.check.loadProperties.LoadProperty;

public class Oracleconnector {
	static boolean isRouted = false;
	public static void main(String[] args) {


	}
	
	public static Connection getConnection(DBConnVO dbConnVO) throws SQLException{
		Connection conn = null;
		
		//Properties prop = LoadProperty.loadConnProp("oracle");
		
		String Driver = dbConnVO.getDriverType();
        String URL = dbConnVO.getUrl();
        String USER = dbConnVO.getUser();
        String Password = dbConnVO.getPassword();
        boolean isRoute = dbConnVO.getIsRoute();
		try {
			if (isRoute&&!isRouted){
				JumpHosts.route2DB(dbConnVO);
				isRouted = true;
			}
			Class.forName(Driver);
			System.out.println("****"+"connected to oracle with user "+USER+"/"+Password+" on "+URL);
			conn =  DriverManager.getConnection(URL, USER, Password);
			SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = Calendar.getInstance().getTime();
			String dateStr = format.format(date);
			System.out.println(dateStr+"****"+"connected to oracle with user "+USER+" on "+URL);

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = Calendar.getInstance().getTime();
			String dateStr = format.format(date);
			System.out.println(dateStr+"****"+"error when get the oracle connection");
			throw e;
		}
		
		return conn;
	}
}
